package com.zhconver;


import com.spreada.utils.chinese.ZHConverter;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author mwl 2018-11-16
 **/
public class SimpleConverUnSimple {
    public static void main(String[] args) {
        //        ZHConverter converter = ZHConverter.getInstance(ZHConverter.SIMPLIFIED);
        //
        //        String simplifiedStr = converter.convert("有背光的機械式鍵盤");
        //        System.out.println(simplifiedStr);
        //
        //        //简体转繁体
        //        System.out.println(ZHConverter.convert("有背光的机adsfadsf23423.jj;;lkj;32j283490047453*(&*&%^%#$械式键盘",
        //                                               ZHConverter.TRADITIONAL));
        //        ;
        //        System.out.println(ZHConverter.convert("機械鍵盤", ZHConverter.TRADITIONAL));
        List<String> rootPaths = new ArrayList<String>();
        rootPaths.add("/Users/mwl/workspace/reader/reader-service/src/main/java/com/");
        rootPaths.add("/Users/mwl/workspace/reader/reader-model/src/main/java/com/");
        rootPaths.add("/Users/mwl/workspace/reader/reader-persistent/src/main/java/com/");
        rootPaths.add("/Users/mwl/workspace/reader/reader-mobile-controller/src/main/java/com/");
        for (String rootPath : rootPaths) {
            File rootFile = new File(rootPath);
            readDirectory(rootFile);
        }
    }

    public static void readDirectory(File file) {
        if (file.isFile()) {
            if (file.getName().endsWith(".java")) {
                readFile(file);
            } else {
                return;
            }
        }
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File file1 : files) {
                readDirectory(file1);
            }
        }
    }

    public static void readFile(File file) {
        String temp = "";
        try {
            FileInputStream fis = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(isr);
            StringBuffer buf = new StringBuffer();


            // 保存该行后面的内容
            while ((temp = br.readLine()) != null) {
                boolean need = temp.matches("^\\s{1,}[a-z,A-Z]{1,}.*");
                need = need && !temp.matches("^\\s{1,}(logger\\.){1}[a-z,A-Z]{1,}.*");
                buf.append(need ? ZHConverter.convert(temp, ZHConverter.TRADITIONAL) : temp).append("\n");
            }
            br.close();
            FileOutputStream fos = new FileOutputStream(file);
            PrintWriter pw = new PrintWriter(fos);
            pw.write(buf.toString().substring(0, buf.toString().length() - 1).toCharArray());
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
